package com.glodon.paas.account.dao.mysql;

import org.springframework.beans.factory.annotation.Autowired;

import com.glodon.paas.account.dao.OAuthTokenDao;
import com.glodon.paas.account.dao.domain.OAuthTokenEntity;
import com.glodon.paas.account.dao.mapper.OAuthTokenEntityMapper;

public class OAuthTokenDaoMysqlImpl implements OAuthTokenDao {
	@Autowired
	private OAuthTokenEntityMapper oAuthTokenEntityMapper;
	
	@Override
	public String insert(OAuthTokenEntity entity) {
		oAuthTokenEntityMapper.insertSelective(entity);
		return entity.getValue();
	}

	@Override
	public OAuthTokenEntity get(String id) {
		return oAuthTokenEntityMapper.selectByPrimaryKey(id);
	}

	@Override
	public int delete(String id) {
		return oAuthTokenEntityMapper.deleteByPrimaryKey(id);
	}

	@Override
	public OAuthTokenEntity readToken(String tokenValue) {
		return oAuthTokenEntityMapper.selectByPrimaryKey(tokenValue);
	}

	@Override
	public void saveToken(OAuthTokenEntity token) {
		OAuthTokenEntity entity = oAuthTokenEntityMapper.selectByPrimaryKey(token.getValue());
		if(entity == null){
			oAuthTokenEntityMapper.insertSelective(token);
		}else{
			oAuthTokenEntityMapper.updateByPrimaryKeySelective(token);
		}
	}

	@Override
	public OAuthTokenEntity removeToken(String tokenValue) {
		OAuthTokenEntity entity = oAuthTokenEntityMapper.selectByPrimaryKey(tokenValue);
		if(entity != null){
			oAuthTokenEntityMapper.deleteByPrimaryKey(tokenValue);
		}
		return entity;
	}

	@Override
	public void removeToken(String consumerKey, String authorizerId) {
		oAuthTokenEntityMapper.deleteByConsumerKeyAuthorizerId(consumerKey,authorizerId);
	}
	
	 

}
